package cn.edu.nsmc.modules.sys.service.impl;

import cn.edu.nsmc.common.utils.R;
import cn.edu.nsmc.modules.sys.dao.SysDepartmentDao;
import cn.edu.nsmc.modules.sys.entity.SysDepartmentEntity;
import cn.edu.nsmc.modules.sys.form.DepartmentList;
import cn.edu.nsmc.modules.sys.form.req.ReqDepartmentForm;
import cn.edu.nsmc.modules.sys.service.SysDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author ZY
 * @Date 2024/2/19 20:09
 */
@Service("sysDepartmentService")
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentDao, SysDepartmentEntity> implements SysDepartmentService {
    @Resource
    private SysDepartmentDao sysDepartmentDao;
    @Override
    public R addDepartment(SysDepartmentEntity sysDepartmentEntity) {
        int i = sysDepartmentDao.insert(sysDepartmentEntity);
        return i == 1 ? R.ok("添加成功") : R.error("添加失败");
    }

    @Override
    public R updateDepartment(SysDepartmentEntity sysDepartmentEntity) {
        int i = sysDepartmentDao.updateDepartment(sysDepartmentEntity);
        return i == 1 ? R.ok("修改成功") : R.error("修改失败");
    }

    @Override
    public R getDepartmentById(Long id) {
        ReqDepartmentForm reqDepartmentForm =  sysDepartmentDao.getDepartmentById(id);
        return reqDepartmentForm != null ? R.success("查询成功", reqDepartmentForm) : R.error("查询失败");
    }

    @Override
    public R getDepartmentList(DepartmentList departmentList) {
        departmentList.setPage((departmentList.getPage() - 1) * departmentList.getSize());
        List<ReqDepartmentForm> list = sysDepartmentDao.getDepartmentList(departmentList);
        Integer total = sysDepartmentDao.getDepartmentListNumber(departmentList);
        R r =  R.page("查询成功", list, total);
        return r;
    }

    @Override
    public R deleteDepartmentByIds(String[] ids) {
        for (String _id : ids) {
            Long aId = Long.valueOf(_id);
            Integer i = sysDepartmentDao.deleteById(aId);
            if (i == 0) {
                return R.error("删除失败");
            }
        }
        return R.ok("删除成功");
    }

    @Override
    public R deleteDepartmentById(Long id) {
        sysDepartmentDao.deleteById(id);
        return R.ok("删除成功");
    }

}
